package cn.com.victorysoft.ylgl.service.fracturingpressure.controller;

import cn.com.victorysoft.ylgl.service.commons.mybatis.paging.bean.DataPaging;
import cn.com.victorysoft.ylgl.service.commons.web.JsonMessage;
import cn.com.victorysoft.ylgl.service.commons.web.RequestContext;
import cn.com.victorysoft.ylgl.service.commons.web.User;
import cn.com.victorysoft.ylgl.service.fracturingpressure.domain.FracturingRelInfo;
import cn.com.victorysoft.ylgl.service.fracturingpressure.domain.FracturingRelInfoVO;
import cn.com.victorysoft.ylgl.service.fracturingpressure.service.fracturing.IFracturingRelInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 压裂相关资料
 */
@Controller
@RequestMapping({"fracturingPressure/fracturingRelInfo"})
public class FracturingRelInfoController {
    private static final Logger logger = LoggerFactory.getLogger(FracturingRelInfoController.class);

    @Resource
    private IFracturingRelInfoService service;

    public FracturingRelInfoController() {
    }

    @RequestMapping(
            value = {"/list.wet"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String list() {
        return "fracturingPressure/fracturingRelInfo/list";
    }

    @RequestMapping(
            value = {"/get.wds"},
            method = {RequestMethod.POST,RequestMethod.GET}
    )
    @ResponseBody
    public JsonMessage get(@RequestParam(value="id", required=true) String id) {
        FracturingRelInfo entity = this.service.getById(id);
        return (new JsonMessage()).success(entity);
    }

    @RequestMapping(
            value = {"/add.wds"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage createEntity(@ModelAttribute("model") FracturingRelInfoVO entity) {
        User user = RequestContext.getCurrentUser();
        entity.setCreateUserId(user.getId());
        entity.setCreateDate(new Date());
        this.service.insert(entity);
        FracturingRelInfoVO newEntity = service.getDtoBy(entity.getDataId());
        return (new JsonMessage()).success(newEntity);
    }


    @RequestMapping(
            value = {"/update.wds"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage updateEntity(@ModelAttribute("model") FracturingRelInfoVO entity) {
        //先根据faid查以前方案的信息
        FracturingRelInfo oldEntity = service.getById(entity.getDataId());
        User user = RequestContext.getCurrentUser();
        //用户需要历史数据
        entity.setUpdateUserId(user.getId());
        entity.setUpdateDate(new Date());
        this.service.updateByPrimaryKey(entity);
        FracturingRelInfoVO newEntity = service.getDtoBy(entity.getDataId());
        return (new JsonMessage()).success(newEntity);
    }


    /**
     * @param ids:1;2;3;
     * @return
     */
    @RequestMapping(value={"/delete.wds"}, method={RequestMethod.POST}, produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonMessage delete(@RequestParam(value="ids", required=true) String ids)
    {
        try
        {
            this.service.deleteByKeys(ids);
            return new JsonMessage().success();
        }
        catch (Exception e)
        {
            logger.error("删除出错: {}", e);
        }
        return new JsonMessage().failure("失败");
    }

    @RequestMapping(
            value = {"/queryPagingList.wds"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @ResponseBody
    public JsonMessage queryPagingList(@RequestParam(value = "page",required = false) String page,
                                       @RequestParam(value = "rows",required = false) String rows,
                                       @RequestParam(value = "yt",required = false) String yt,
                                       @RequestParam(value = "qk",required = false) String qk,
                                       @RequestParam(value = "dk",required = false) String dk,
                                       @RequestParam(value = "cw",required = false) String cw,
                                       @RequestParam(value = "jh",required = false) String jh) {

        DataPaging<FracturingRelInfoVO> dataPaging =  this.service.queryPagingList(yt,qk,dk,cw,jh,rows,page);
        return (new JsonMessage()).success(dataPaging);
    }

}